<?php

namespace App\Support;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\SimpleType\Jc;
class Word
{
    function he($documentPath,$imagePath) {
// 创建 PhpWord 实例并加载文档
        $phpWord = new PhpWord();
        $document = IOFactory::load($documentPath);
        // 获取Word文档的页数
        $pageCount = $document->getSections()->count();
        dd($pageCount);

// 获取文档中的第一个节
        $section = $document->getSections()[0];


// 获取页面的宽度和高度
        $pageWidth = $section->getPageWidth();
        $pageHeight = $section->getPageHeight();

// 添加图片
        //$imagePath = 'path/to/your/image.jpg'; // 替换为你的图片路径
        $imageWidth = 200;  // 图片宽度
        $imageHeight = 200; // 图片高度

// 计算图片位置（右下角）
        $imageLeft = $pageWidth - $imageWidth;
        $imageTop = $pageHeight - $imageHeight;

// 添加图片到文档
        $section->addImage(
            $imagePath,
            array(
                'width' => $imageWidth,
                'height' => $imageHeight,
                'wrappingStyle' => 'square',
                'positioning' => 'absolute',
                'marginLeft' => $imageLeft,
                'marginTop' => $imageTop
            )
        );

// 保存修改后的文档
        $filename = 'document_with_added_image.docx';
        $writer = IOFactory::createWriter($document, 'Word2007');
        $writer->save($filename);


    }
}
